<?php

class CartFace extends FaceBase
{
	private $nohf = ['add','delete'];
	private $api = null;

	public function __construct()
	{
		parent::__construct();
		if(!in_array(N_ACTION,$this->nohf)){
			n_mod('skin')->header();
		}
		$this->mysql = n_api('mysql')->cname('weixin');
		$this->api = n_api('cart');
	}

	public function add()
	{
		$param = $this->param('wxid:char:empty');
		$wxid = $param['wxid'];
		
		$ppid = $_SESSION['ppid'];
		if(empty($ppid)){
			echo json_encode(n_state(false,"请登录！"));
			exit();
		}
		$data = [
			'wxid' => $wxid,
			'ppid' => $ppid,
			'ctime' => time()
		];

		$this->mysql->insert('cart_main', $data);
		echo json_encode(n_state(true,"添加成功"));
	}

	public function delete()
	{
		$param = $this->param('wxid:char:empty');
		$wxid = $param['wxid'];

		$ppid = $_SESSION['ppid'];
		if(empty($ppid)){
			echo json_encode(n_state(false,"请登录！"));
			exit();
		}

		$query = "delete from cart_main where ppid = $ppid and wxid = '$wxid'";
		$this->mysql->exec($query);
		echo json_encode(n_state(true,"删除成功"));
		
	}

	public function mycartOld()
	{
		$ppid = $_SESSION['ppid'];
		$cart = $this->api->getCartByPpid($ppid);
		$cartDetail = n_api('weixin')->getWxDetail($cart);
		foreach ($cartDetail as $value) {
			$cartDetailRs[$value['wxid']] = $value;
		}
		// echo "<pre>";
		// var_dump($cartDetailRs);
		$this->_html('mycart', $cartDetailRs);
	}
	
	
	public function mycart(){
		$param = $this->param('
			cid:uint:ignore,
			date:char:ignore
		');
		if (isset($param['date'])) {
			$date = strtotime($param['date']);
		} else {
			$date = n_api('mprank')->getMaxRankDay();
		}
		$cart = $this->api->getCartByPpid($_SESSION['ppid']);
		$wx = n_api('weixin')->getwxInfo($cart);
		if(!empty($param['cid'])){
			
			foreach($wx as $key=>$val){
				if($val['cid']!=$param['cid']){
					$k = array_search($val['wxid'],$cart);
					unset($cart[$k]);
				}
			}
			$data['cid'] = $param['cid'];
		}
		$data['cart'] = $cart;
		if(!empty($cart)){
			$data['rank_mplist'] = n_api('mprank')->getMprankByWxids($cart,$date);
		}
		$data['date'] = $date;

		$this->_html('mycart', $data);
	}
	public function mycartWeekly(){
		$param = $this->param('
			cid:uint:default[0],
			date:char:ignore
		');
		$maxWeek = n_api('mprank')->getMaxRankDay();
		if (isset($param['date'])) {
			$dates = explode(' - ',$param['date']);
			$sdate = strtotime($dates[0]);
			$edate = strtotime($dates[1]);
		} else {
			$edate = $maxWeek;
			$sdate = $maxWeek - 7*24*60*60;
		}


		$cart = $this->api->getCartByPpid($_SESSION['ppid']);
		if(!empty($param['cid'])){
			
			$wx = n_api('weixin')->getwxInfo($cart);
			foreach($wx as $key=>$val){
				if($val['cid']!=$param['cid']){
					$k = array_search($val['wxid'],$cart);
					unset($cart[$k]);
				}
			}
			$data['cid'] = $param['cid'];
		}
		
		$data['cart'] = $cart;
		if(!empty($cart)){
			$data['rank_mplist'] = n_api('mprank')->getMprankWeeklyByWxidsBetween($cart,$sdate,$edate);
		}

		$data['sdate'] = date('m/d/Y',$sdate);
		$data['edate'] = date('m/d/Y',$edate);
		$data['mdate'] = date('m/d/Y',$maxWeek);
		
		$data['date'] = $date;

		$this->_html('mycartWeekly', $data);
	}	
	public function __destruct(){
		//加入标准尾。
		if(!in_array(N_ACTION,$this->nohf) && 'page'==N_TYPE){
			n_mod('skin')->footer();
		}
	}
}